// pages/orderDetails/orderDetails.js
var app = getApp()
const $api = require('../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 1, //1表示不显示    0表示显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '全部订单', //导航栏 中间的标题
      bg: '#fff', //导航背景
    },
    goodsListProduct: false,//商品详情的展开折叠
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var _this = this;
    _this.setData({
      order_id: options.orderid,
      shop_id: options.shop_id
    })

  },
  onShow() {
    var _this = this;
    //订单详情
    _this.setData({
      loadModal: true
    })
    $api.getOrderDetail({
      login_token: wx.getStorageSync('login_token'),
      order_id: _this.data.order_id
    }).then((res) => {
      // res.data.data.order_info.status = 1
      let box1img = res.data.data.order_info.status == 0 ? 'https://jiujinimg.tjnanguyi.com/zhenyuanqi/dengdaimaijiafukuan.png' :
        res.data.data.order_info.status == 1 && res.data.data.all_asale == 1 ? 'https://jiujinimg.tjnanguyi.com/zhenyuanqi/dengdaishangjiafahuo.png' :
          res.data.data.order_info.status == 2 ? 'https://jiujinimg.tjnanguyi.com/zhenyuanqi/dengdaimaijiashouhuo.png' :
            res.data.data.order_info.status == 3 ? 'https://jiujinimg.tjnanguyi.com/zhenyuanqi/yiwancheng.png' :res.data.data.order_info.status == 1 && res.data.data.all_asale == 0 ? 'https://jiujinimg.tjnanguyi.com/zhenyuanqi/dengdaishangjiafahuo.png' : ''
      let box1title = res.data.data.order_info.status == 0 ? '等待买家付款' :
        res.data.data.order_info.status == 1 && res.data.data.all_asale == 1 ? '等待商家发货' :
          res.data.data.order_info.status == 2 ? '等待买家收货' :
            res.data.data.order_info.status == 3 ? '已完成' :res.data.data.order_info.status == 1 && res.data.data.all_asale == 0 ?'退款中': ''
      _this.setData({
        orderDetsils: res.data.data,
        loadModal: false,
        box1img, box1title
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  //去支付
  onWxPay() {
    var _this = this;
    _this.setData({
      loadModal: true
    })
    wx.navigateTo({
      url: '/pages/Payment/Payment?upType=orderDetails&order_id=' + _this.data.orderDetsils.order_info.id,
    })
    _this.setData({
      loadModal: false
    })
    return
    //支付
    wx.login({
      success(res) {
        console.log(res)

        $api.getOpenidAppPay({
          js_code: res.code
        }).then((res) => {
          console.log(res.data, 66)
          if (res.data.code == 0) {
            var openid = res.data.data.openid;
            $api.appPayFunc({
              login_token: wx.getStorageSync('login_token'),//用户登录token
              order_id: _this.data.orderDetsils.order_info.id,//订单id
              pay_type: 4,//支付类型 4 第三方微信支付 
              is_fp: 0,//是否分批付款 0否 1是
              // fp_money: options.fp_money,//付款金额
              sf_openid: openid,//三方支付微信支付的openid

            }).then((res) => {
              if (res.data.code == 0) {
                var payData = JSON.parse(res.data.data.sfwxdata.expend.pay_info);
                var packages = payData.package;
                var timeStamp = payData.timeStamp;
                wx.requestPayment({
                  timeStamp: timeStamp.toString(),
                  nonceStr: payData.nonceStr,
                  package: packages,
                  signType: payData.signType,
                  paySign: payData.paySign,
                  success(res) {
                    console.log(res)
                    //支付成功
                    _this.onShow()
                  },
                  fail(res) {
                    console.log(res)
                    _this.setData({
                      loadModal: false
                    })
                  },
                })

              } else {
                wx.showToast({
                  title: res.data.message,
                  icon: 'none',
                  duration: 2000
                })
              }
            })
          }
        })
      }
    })
    // $api.wxPay({
    //   login_token: wx.getStorageSync('login_token'),
    //   order_id: _this.data.orderDetsils.order_info.id
    // }).then((res) => {
    //   var wxPay = res.data.data;
    //   var timeStamp = wxPay.timeStamp
    //   wx.requestPayment({
    //     timeStamp: timeStamp.toString(),
    //     nonceStr: wxPay.nonceStr,
    //     package: wxPay.package,
    //     signType: wxPay.signType,
    //     paySign: wxPay.paySign,
    //     success(res) {
    //       console.log(res)
    //       _this.setData({
    //         ['orderDetsils.order_info.status']: 1,
    //         loadModal: false
    //       })
    //     },
    //     fail(res) {
    //       console.log(res)
    //       _this.setData({
    //         loadModal: false
    //       })
    //     }
    //   })


    // }).catch((err) => {
    //   _this.setData({
    //     loadModal: false
    //   })
    // })
  },
  //修改地址
  onModifyAddress() {
    var _this = this;
    var status = _this.data.orderDetsils.order_info.status;
    if (status == 0) {
      wx.navigateTo({
        url: '/pages/address/modify/modify?order=1&orderid=' + _this.data.orderDetsils.order_info.id,
      })
    }
  },
  //查看物流
  onLogistics(e) {
    let orderDetsils = this.data.orderDetsils
    if (orderDetsils.order_info.status == 1 && orderDetsils.all_asale == 1) {
      wx.showToast({
        title: '暂无物流信息',
        icon: 'none',
        duration: 2000
      })
      return
    }

    wx.navigateTo({
      url: '/pages/logistics/logistics?express_id=' + e.currentTarget.dataset.id + '&express_name=' + e.currentTarget.dataset.express_name + '&express_no=' + e.currentTarget.dataset.express_no,
    })
  },
  //确认收货
  onReceiving() {
    var _this = this;
    _this.setData({
      loadModal: true
    })
    $api.confirmDelivery({
      login_token: wx.getStorageSync('login_token'),
      order_id: _this.data.orderDetsils.order_info.id
    }).then((res) => {
      _this.setData({
        loadModal: false,
        ['orderDetsils.order_info.status']: 3
      })
      wx.showToast({
        title: '收货完成',
        icon: 'none',
        duration: 2000
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  //取消订单
  onWithdraw(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  //确认取消
  onConfirmCancel() {
    var _this = this;
    _this.setData({
      modalName: null,
      loadModal: true
    })
    var is_register = _this.data.orderDetsils.order_info.is_register;
    //is_register   是否是申请店铺的订单 1是0不是  2升级订单
    if (is_register == 1) {
      $api.cancleShopInfo({
        login_token: wx.getStorageSync('login_token'),
        shop_id: _this.data.orderDetsils.order_info.shop_id ? _this.data.orderDetsils.order_info.shop_id : _this.data.shop_id
      }).then((res) => {
        _this.setData({
          loadModal: false
        })
        wx.navigateTo({
          url: '/pages/index/index',
        })
        wx.setStorageSync('is_prod_user', 0);
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    } else if (is_register == 2) {
      console.log(_this.data.houseData)
      $api.cancleUpgrade({
        login_token: wx.getStorageSync('login_token'),
        shop_id: _this.data.orderDetsils.order_info.shop_id ? _this.data.orderDetsils.order_info.shop_id : _this.data.shop_id,
        order_id: _this.data.orderDetsils.order_info.id,

      }).then((res) => {
        _this.setData({
          loadModal: false,
          modalName: null
        })
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/index/index',
          })
        }, 1000)


      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    } else {
      $api.delOrder({
        login_token: wx.getStorageSync('login_token'),
        order_id: _this.data.orderDetsils.order_info.id
      }).then((res) => {
        _this.setData({
          loadModal: false
        })
        if (_this.data.orderDetsils.order_info.status == 0) {
          wx.navigateTo({
            url: '/pages/index/index',
          })
          wx.setStorageSync('is_prod_user', 0);
        } else {
          $api.getOrderDetail({
            login_token: wx.getStorageSync('login_token'),
            order_id: _this.data.orderDetsils.order_info.id
          }).then((res) => {
            _this.setData({
              orderDetsils: res.data.data,
              loadModal: false
            })
            wx.navigateTo({
              url: '/pages/index/index',
            })
            wx.setStorageSync('is_prod_user', 0);
          }).catch((err) => {
            _this.setData({
              loadModal: false
            })
          })
        }

      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    }



  },
  //商品详情的展开折叠
  onGoodsListProduct() {
    this.setData({
      goodsListProduct: !(this.data.goodsListProduct)
    })
  },
  //提醒发货
  onRemind() {
    var _this = this;
    _this.setData({
      loadModal: true
    })
    setTimeout(function () {
      _this.setData({
        loadModal: false
      })
      wx.showToast({
        title: '已提醒商家，商家会尽快安排发货',
        icon: 'none',
        duration: 2000
      })
    }, 500)

  },
  //申请退款
  onRefund(e) {
    var _this = this;
    var id = e.currentTarget.dataset.id;
    var list = _this.data.orderDetsils.order_detail_info;
    var before = e.currentTarget.dataset.before; //1==发货前  2==发货后
    if (list.length == 1) {
      _this.setData({
        loadModal: true
      })
      $api.selectOrdersGoods({
        login_token: wx.getStorageSync('login_token'),
        order_id: id
      }).then((res) => {
        let goods = []
        res.data.data.forEach((item) => {
          if (item.asale_status == 0) {
            goods.push(item)
          }
        })
        if (goods.length == 0) {
          wx.showToast({
            title: "无售后商品",
            icon: 'none',
            duration: 2000
          })
          return
        }
        res.data.data[0].icon = false
        wx.setStorageSync('goods', goods);
        wx.setStorageSync('price', res.data.data[0].price);
        _this.setData({
          loadModal: false
        })

      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
      //发货前
      if (before == 1) {
        wx.navigateTo({
          url: '/pages/sale/refund/refund',
        })

      }
      //发货后
      if (before == 2) {
        wx.navigateTo({
          url: '/pages/sale/sale/sale',
        })

      }



    }
    if (list.length > 1) {
      wx.navigateTo({
        url: '/pages/sale/detail/detail?id=' + id + '&price=' + e.currentTarget.dataset.price + '&before=' + before,
      })

    }



  },
})